package com.base.schema.singleton;

/**
 * 懒汉式6 枚举（线程安全，还可以防止反序列化重新生成的对象，推荐使用）这个方法被大佬推荐使用
 * @ClassName: SingletonDemo7
 * @Description: 
 * @Author xuyou
 * @DateTime 2021年3月2日 上午10:59:43
 */
public class SingletonDemo8 {
  
	public SingletonDemo8() {};
	
	enum SingletonDemo{
		INSTENCE;
		public void print(){
	        System.out.println("这是通过枚举获得的实例");
	        System.out.println("SingletonDemo.pring()");
	    }
	}
	
	public static void main(String[] args) {
		//单线程获取无影响
//		SingletonDemo3 instence1 = SingletonDemo3.getInstence();
//		SingletonDemo3 instence2 = SingletonDemo3.getInstence();
//		System.out.println(instence1 == instence2);
		
		//模拟线程不安全
		for (int i = 0; i < 10; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					SingletonDemo instence1 = SingletonDemo8.SingletonDemo.INSTENCE;	
					System.out.println(instence1);
					instence1.print();
				}
			}).start();
		}
		
	}
	
}
